package com.ly.publish;

import com.ly.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.BuiltinExchangeType;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    //交换机名称
    static final String FANOUT_EXCHAGE = "fanout_exchange";
    //队列名称
    static final String FANOUT_QUEUE_1 = "fanout_queue_1";
    static final String FANOUT_QUEUE_2 = "fanout_queue_2";
    //队列名称

    public static void main(String[] args) {
        //获取连接
        Connection connection = ConnectionUtil.getConnection();
        //创建频道
        Channel channel = null;
        try {
            channel = connection.createChannel();
            //声明交换机！
            channel.exchangeDeclare(FANOUT_EXCHAGE, BuiltinExchangeType.FANOUT);

            //声明队列
            channel.queueDeclare(FANOUT_QUEUE_1, false, false, false, null);
            channel.queueDeclare(FANOUT_QUEUE_2, false, false, false, null);

            //绑定队列与交换机
            channel.queueBind(FANOUT_QUEUE_1,FANOUT_EXCHAGE,"");
            channel.queueBind(FANOUT_QUEUE_2,FANOUT_EXCHAGE,"");
            String message = "hello world 3";

            channel.basicPublish(FANOUT_EXCHAGE,"",null,message.getBytes());
            System.out.println("发送成功！");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }
    }
}
